/*****Creat on 2012-04-05***/
/*****equation_test.cpp*****/
/********第二次作业**********/



#include <iostream>
#include <cmath>
using namespace std;

void equation1(double M,double N)                           /****求解一次方程函数*****/
{
   	double y;
	cout <<"Now the equation is "<<M<<"*X+"<<N<<"=0"<<endl;
	if (M==0)
	    cout <<"The equation is no solution."<<endl;
	else 
	    y=-N/M;
	cout <<"x="<<y<<endl;
}

void equation2(double M,double N,double O)                 /****求解二次方程的函数*****/
{
	
	if (M==0)
	    equation1(N,O);
	else 
	{
		cout <<"Now the equation is "<<M<<"*X^2+"<<N<<"*X+"<<O<<"=0"<<endl;
		double f,x1,x2;
		f=N*N-4*M*O;
		if (f>=0)
		{
			cout <<"Equation solution exists real number."<<endl;
			x1=(-N+sqrt(f))/2*M;
			x2=(-N-sqrt(f))/2*M;
			cout <<"x1="<<x1<<endl;
			cout <<"x2="<<x2<<endl;
		}
		else
		{	
			cout <<"Equation has two plural solution."<<endl;
		    cout <<"x1="<<(-N/2*M)<<"+"<<(sqrt(-f)/2*M)<<"j"<<endl;
			cout <<"x2="<<(-N/2*M)<<"-"<<(sqrt(-f)/2*M)<<"j"<<endl;
		}
    }
}

void equation3(double M,double N,double O,double P,double real_x1,double real_x2,double real_x3,double imag_x1,double imag_x2,double imag_x3)
{                                                                             /******卡丹公式求解三次方程************/
	double y,q,r,u,v,g,h,f;
    y=(3.0*M*O-N*N)/(3*M*M);
    q=(2.0*pow(N,3.0)-9*M*N*O+27.0*M*M*P)/(27.0*pow(M,3.0));
    r=N/(3.0*M);
    h=pow(q/2.0,2.0)+pow(y/3.0,3.0);
    g=sqrt(h);
    if(h>=0)
    {
		if(-q/2.0+g<0)
            u=-pow(fabs(-q/2.0+g),1.0/3.0);
        else
            u=pow((-q/2.0+g),1.0/3.0);
        if(-q/2.0-g<0)
            v=-pow(fabs(-q/2.0-g),1.0/3.0);
        else
            v=-pow((-q/2.0-g),1.0/3.0);                                       
        if(h==0)                                                              /********存在三个实根，其中有一个二重根*****/
        {
			real_x1=u+v-r;            imag_x1=0;
            real_x2=-(u+v)/2-r;       imag_x2=0;
            real_x3=-(u+v)/2-r;       imag_x3=0;
        }
        else                                                                   /******有一个实根，一对共轭虚根(h>0)*******/
        {
			real_x1=u+v-r;       imag_x1=0;
            real_x2=-(u+v)/2;    imag_x2=sqrt(3.0)*(u-v)/2;
            real_x3=-(u+v)/2;    imag_x3=-sqrt(3.0)*(u-v)/2;
        }
     }
     else                                                                      /****存在三个不相等的实根(h<0)**********/
     {
		 f=acos((-q/2)/(sqrt(pow(fabs(y),3)/27)));
         real_x1=2*sqrt(fabs(y)/3.0)*cos(f/3.0)-r;
         real_x2=-2*sqrt(fabs(y)/3.0)*cos((f+3.1415926)/3.0)-r;
         real_x3=-2*sqrt(fabs(y)/3.0)*cos((f-3.1415926)/3.0)-r;
         imag_x1=0;  
		 imag_x2=0;    
		 imag_x3=0;
     }
	 cout <<"x1="<<real_x1<<endl;
     cout <<"x2="<<real_x2<<"+"<<imag_x2<<"j"<<endl;
	 cout <<"x3="<<real_x3<<""<<imag_x3<<"j"<<endl;
}
	
int main()
{ 
	int i;
	cout <<"Please enter the degree of the equation(1-3):";
	cin >>i;
	if (i==1)
	{
		double A,B;
		cout <<"Enter the factor:"<<endl;
	    cout <<"A=";
	    cin >>A;
	    cout <<"B=";
	    cin >>B;
		equation1(A,B);
    }
	else if (i==2)
	{
		double A,B,C;
	    cout <<"Enter the factor:"<<endl;
	    cout <<"A=";
	    cin >>A;
	    cout <<"B=";
	    cin >>B;
	    cout <<"C=";
	    cin >>C;
		equation2(A,B,C);
	}
	else if (i==3)
	{
		double A,B,C,D;
        double real_x1,real_x2,real_x3;
        double imag_x1,imag_x2,imag_x3;
	    cout <<"Enter the factor:"<<endl;
	    cout <<"A=";
	    cin >>A;
	    cout <<"B=";
	    cin >>B;
	    cout <<"C=";   
	    cin >>C;  
	    cout <<"D="  ;
	    cin >>D;
        equation3(A,B,C,D,real_x1,real_x2,real_x3,imag_x1,imag_x2,imag_x3);
	}
	return 0;
}
